package com.phone.servlet;

import com.phone.ParameterCheck;
import com.phone.entity.Result;
import com.phone.exception.ErrorCode;
import com.phone.exception.ErrorException;
import com.phone.handle.AccountHandle;
import com.phone.handle.UserHandle;
import org.hibernate.Session;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;

/**
 * Created by tisen on 2017/9/22.
 */
@WebServlet(name = "Register", urlPatterns = "/register")
public class RegisterServlet extends SuperServlet {
    private static final String PHONE_REGISTER = "phone";
    private static final String USER_REGISTER = "user";
    private static final String WX_REGISTER = "wx";
    private static final String QQ_REGISTER = "qq";

    private static final String PHONE = "phone";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String WX_OPENID = "wx_openid";
    private static final String QQ_OPENID = "qq_openid";
    private static final String NICKNAME = "nickname";
    private static final String AVATAR = "avatar";

    @Override
    protected JSONObject Control(ParameterCheck check, Session session) throws Exception {
        JSONObject jsonObject = new JSONObject();

        String action = check.getParameterString(ACTION, false);
        switch (action) {
            case PHONE_REGISTER:
                jsonObject = phone_register(check, session);
                break;
            case USER_REGISTER:
                jsonObject = user_register(check, session);
                break;
            case WX_REGISTER:
                jsonObject = wx_register(check, session);
                break;
            case QQ_REGISTER:
                jsonObject = qq_register(check, session);
                break;
            default:
                throw new ErrorException(ErrorCode.UN_DEFINED_ACTION);
        }

        return jsonObject;
    }

    private JSONObject qq_register(ParameterCheck check, Session session) throws Exception {
        String qq = check.getParameterString(QQ_OPENID, false);
        String nickname = check.getParameterString(NICKNAME, true);
        String avatar = check.getParameterString(AVATAR, true);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("qq register successful");
        result.setObject(new AccountHandle(session).qq_register(qq, nickname, avatar));
        return new JSONObject(result);
    }

    private JSONObject wx_register(ParameterCheck check, Session session) throws Exception {
        String wx = check.getParameterString(WX_OPENID, false);
        String nickname = check.getParameterString(NICKNAME, true);
        String avatar = check.getParameterString(AVATAR, true);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("wx register successful");
        result.setObject(new AccountHandle(session).wx_register(wx, nickname, avatar));
        return new JSONObject(result);
    }

    private JSONObject user_register(ParameterCheck check, Session session) throws Exception {
        return null;
    }

    private JSONObject phone_register(ParameterCheck check, Session session) throws Exception {
        String phone = check.getParameterString(PHONE, false);
        String username = check.getParameterString(USERNAME, true);
        String password = check.getParameterString(PASSWORD, true);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("phone register successful");
        result.setObject(new AccountHandle(session).phone_register(phone, username, password));
        return new JSONObject(result);
    }

}
